<?php
declare(strict_types=1);
namespace Service\VenderClient\Hanshow;
use Service\Code\Normal;
use Service\BaseService;
use Service\ExtraService\TaskWorker;
use Service\Code\CodeBuilder;

class HanshowApi extends BaseService
{
    protected $config;
    public function __construct( array $configBody = [] )
    {
        $this->config = $configBody;
        if(empty($this->config['storeCode'])||empty($this->config['customerStoreCode'])||empty($this->config['url'])){
            throw new \Exception('处理失败【汉朔未配置，请联系管理员配置】 ' );
        }
    }
    
    public function UpdateGoods($mnsid,$param) {
        foreach ($param as &$item ) {
            $item['IIS_COMMAND']= 'UPDATE';
            $item['customerStoreCode']= $this->config['customerStoreCode'];
            $item['storeCode']= $this->config['storeCode'];
            unset($item);
        }
        $postdata=[
            'customerStoreCode'=>$this->config['customerStoreCode'],
            'storeCode'=>$this->config['storeCode'],
            'batchNo'=>CodeBuilder::getCode($mnsid),
            'callbackUrl'=>'',
            'items'=>$param
        ];
        $response=$this->execute('UpdateGoods', $postdata);
        return $response;
    }
    
    public function execute(string $method, array $data){
        $parameter = [
            [
                'tag'       => 'hanshow_'.$method,
                'url'       => $this->config['url'],
                'data'      => $data,
                'method'    => 'post',
                'format'    => 'json',
                'header'    => [
                ],
            ],
        ];
        $resTask    = TaskWorker::instance()->addTask($parameter);
        $resTask    = current($resTask);
        $response   = @json_decode($resTask['response']['result'],true);
        if(isset($response['resultCode'])&&$response['resultCode']!=1001){
            return Normal::returnCode(Normal::FAIL,$response['message'],$response);
        }
        return Normal::returnCode(Normal::SUC,"请求成功",$response);
    }
}
